#import "FlutterDesPlugin.h"
#import "DesEncrypt.h"

@implementation FlutterDesPlugin
+ (void)registerWithRegistrar:(NSObject<FlutterPluginRegistrar>*)registrar {
    FlutterMethodChannel* channel = [FlutterMethodChannel
        methodChannelWithName:@"flutter_des"
              binaryMessenger:[registrar messenger]];
    FlutterDesPlugin* instance = [[FlutterDesPlugin alloc] init];
    [registrar addMethodCallDelegate:instance channel:channel];
}


- (void)handleMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result {
    NSLog(@"call.method:%@  arguments:%@",call.method,call.arguments);
    NSArray *arguments = call.arguments;
    if(arguments.count < 3){
        result(nil);
        return;
    }
    NSString *key = arguments[1];
    NSString *iv = arguments[2];
    if ([@"encrypt" isEqualToString:call.method]) {
        //NSString *str = [DesEncrypt encryptText:arguments[0] key:key iv:iv];
        NSData *data = [DesEncrypt desDealData:[arguments[0] dataUsingEncoding:NSUTF8StringEncoding] key:key iv:iv operation:kCCEncrypt ];//kCCDecrypt

        //+ (NSData *)desDealData:(NSData *)data key:(NSString*)key iv:(NSString *)iv operation:(CCOperation)encryptOperation
        //NSLog(@"%@",str);
        result(data);
    }else if ([@"encryptToHex" isEqualToString:call.method]) {
        NSString *str = [DesEncrypt encryptText:arguments[0] key:key iv:iv];
        result(str);
    }else if ([@"decrypt" isEqualToString:call.method]) {
        NSData *data = [DesEncrypt desDealData:[(FlutterStandardTypedData*)arguments[0] data] key:key iv:iv operation:kCCDecrypt ];
        result([[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]);
    }
  else {
    result(FlutterMethodNotImplemented);
  }
}


@end
